import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.security.KeyStore.Builder;
import java.util.ArrayList;
import java.util.HashSet;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


public class GenerateParamFile {
	//The csv file path containing the network data
	private static String dataInput = "c:/socialEnvironment/PopulationDataSet-100-Network.csv";
	//The output file path
	private static File outputFolder = new File("c:/socialEnvironment/walkthrough-media");

	//private static String whatToGenerateString = "general,cf,context,actor,contact,event,message";
	private static String whatToGenerateString = "cf,general";

	public static void main(String[] args) throws IOException{
		ArrayList<String> whatToGenerateList = new ArrayList<String>();
		String[] whatToGenerateTab = whatToGenerateString.split(",");
		for(int i = 0; i < whatToGenerateTab.length; i++){
			whatToGenerateList.add(whatToGenerateTab[i]);
		}

		if(whatToGenerateList.contains("general")){
			buildGeneral();
		}
		if(whatToGenerateList.contains("cf")){
			buildCF();
		}
		if(whatToGenerateList.contains("context")){
			buildContext();
		}
		if(whatToGenerateList.contains("actor")){
			buildActors();
		}
		if(whatToGenerateList.contains("contact")){
			buildContact();
		}
		if(whatToGenerateList.contains("event")){
			buildEvent();
		}
		if(whatToGenerateList.contains("message")){
			buildMessage();
		}
	}

	public static Element addRole(Element elt, String name, String comment){
		Element role = elt.addElement("role");
		role.addAttribute("name", name);
		if(comment!=null && !comment.isEmpty())
			role.addComment(comment);
		return role;
	}

	public static Element addContextModelRoleSalience(Element elt, String name, String salience, String[] actions, String comment){
		Element contextRole = elt.addElement("role");
		contextRole.addAttribute("name", name);
		contextRole.addAttribute("salience", salience);
		if(actions != null){
			for(int i=0; i<actions.length; i++){
				Element action = contextRole.addElement("actionModel");
				action.addAttribute("name", actions[i]);
			}
		}
		if(comment!=null && !comment.isEmpty())
			contextRole.addComment(comment);
		return contextRole;
	}

	public static Element addActionModel(Element elt, String name,String verb, String[] roles, String comment){
		Element actionModel = elt.addElement("actionModel");
		actionModel.addAttribute("name", name);
		actionModel.addAttribute("verb", verb);
		for(int i=0; i<roles.length; i++){
			Element actionRole = actionModel.addElement("role");
			actionRole.addAttribute("name", roles[i]);
		}
		if(comment!=null && !comment.isEmpty())
			actionModel.addComment(comment);
		return actionModel;
	}

	public static Element addContext(Element elt, String name, String eventImpactLevelName, String comment){
		Element contextModel = elt.addElement("context");
		contextModel.addAttribute("name", name);
		contextModel.addAttribute("eventImpactLevel", eventImpactLevelName);
		if(comment!=null && !comment.isEmpty())
			contextModel.addComment(comment);
		return contextModel;
	}


	public static Element addPayoff(Element payoffs, String performerRole, String value,
			String observerRole, String actionName, String comment){
		Element payoff = payoffs.addElement("payoff");
		payoff.addAttribute("performerRole", performerRole);
		payoff.addAttribute("value", value);
		payoff.addAttribute("observerRole", observerRole);
		payoff.addAttribute("action", actionName);
		if(comment!=null && !comment.isEmpty())
			payoff.addComment(comment);
		return payoff;
	}
	
	public static Element addEmptyPayoffToContextModel(Element context){
		Element payoff = context.addElement("payoffs");
		return payoff;
	}
	
	public static Element addActorContext(Element actorContexts, String contextName){
		Element context = actorContexts.addElement("context");
		context.addAttribute("name", contextName);
		return context;
	}
	
	
	
	public static Element addEvent(Element events, String eventName, 
			String eventDescription, String contextName, String eventDate,
			String eventIsDecisionEvent, String eventObserver, String eventEmitter, 
			String eventDecisionMaker, String comment){
		Element event = events.addElement("event");
		event.addAttribute("name", eventName);
		event.addAttribute("descritpion", eventDescription);
		event.addAttribute("context", contextName);
		event.addAttribute("date", eventDate);
		event.addAttribute("isDecisionEvent", eventIsDecisionEvent);
		event.addAttribute("observer", eventObserver);
		event.addAttribute("emitter", eventEmitter);
		event.addAttribute("decisionMaker", eventDecisionMaker);
		if(comment!=null && !comment.isEmpty())
			event.addComment(comment);
		return event;
	}
	
	public static Element addactorActionRolePayoffToEvent(Element event, String eventActor, String actionName, String roleName, String eventPayoff){
		Element actorActionRolePayoff = event.addElement("actorActionRolePayoff");
		actorActionRolePayoff.addAttribute("actorName", eventActor);
		actorActionRolePayoff.addAttribute("actionName", actionName);
		actorActionRolePayoff.addAttribute("roleName", roleName);
		actorActionRolePayoff.addAttribute("payoff", eventPayoff);
		return actorActionRolePayoff;
	}
	
	public static Element addForceActor(Element forceActors, String name, String role){
		Element forceActor = forceActors.addElement("forceActor");
		forceActor.addAttribute("name", name);
		forceActor.addAttribute("role", role);
		return forceActor;
	}
	
	public static void buildGeneral() throws IOException{

		File outputFile = new File(outputFolder,outputFolder.getName()+"General.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("generalParameters");
		
		//Deadline for ending the simulation
		Element deadline = root.addElement("deadline");
		deadline.addAttribute("nbTick", "100");
		
		//Attitude delta threshold for ending the simulation
		Element deltaThreshold = root.addElement("deltaThresholdForEnding");
		deltaThreshold.addAttribute("value", "0.05");		
		Element nothingThreshold = root.addElement("nothingThreshold");
		nothingThreshold.addAttribute("nbTick", "20");
		
		//Time
		Element time = root.addElement("time");
		time.addAttribute("tickDurationInSecond", "43200");

		//Seeds
		Element randomSeeds = root.addElement("randomSeeds");
		Element classSeed1 = randomSeeds.addElement("classSeed");
		classSeed1.addAttribute("className", "CommunicationExtension");
		classSeed1.addAttribute("seed", "123156789");
		Element classSeed2 = randomSeeds.addElement("classSeed");
		classSeed2.addAttribute("className", "SocialLearningExtension");
		classSeed2.addAttribute("seed", "123156789");
		Element classSeed3 = randomSeeds.addElement("classSeed");
		classSeed3.addAttribute("className", "ActiveContext");
		classSeed3.addAttribute("seed", "123156789");
		Element classSeed4 = randomSeeds.addElement("classSeed");
		classSeed4.addAttribute("className", "Trace");
		classSeed4.addAttribute("seed", "123156789");
		Element classSeed5 = randomSeeds.addElement("classSeed");
		classSeed5.addAttribute("className", "Event");
		classSeed5.addAttribute("seed", "123156789");
		Element classSeed6 = randomSeeds.addElement("classSeed");
		classSeed6.addAttribute("className", "ActorDirectory");
		classSeed6.addAttribute("seed", "123156789");

		//Random Flags
		Element randomFlags = root.addElement("randomFlags");
		Element communiactionExtension = randomFlags.addElement("CommunicationExtension");
		Element filterMessages = communiactionExtension.addElement("randomFlag");
		filterMessages.addAttribute("flagName", "filterMessages");
		filterMessages.addAttribute("value", "false");
		Element buildAddresseesList = communiactionExtension.addElement("randomFlag");
		buildAddresseesList.addAttribute("flagName", "buildAddresseesList");
		buildAddresseesList.addAttribute("value", "false");


		Element socialLearning = randomFlags.addElement("SocialLearning");
		Element sendOnProcessMessageAttitudeChange = socialLearning.addElement("randomFlag");
		sendOnProcessMessageAttitudeChange.addAttribute("flagName", "sendOnProcessMessageAttitudeChange");
		sendOnProcessMessageAttitudeChange.addAttribute("value", "false");

		Element activeContext = randomFlags.addElement("ActiveContext");
		Element getOrCreateAttitude = activeContext.addElement("randomFlag");
		getOrCreateAttitude.addAttribute("flagName", "getOrCreateAttitude");
		getOrCreateAttitude.addAttribute("value", "false");
		Element updateAttitudesMessageCreation = activeContext.addElement("randomFlag");
		updateAttitudesMessageCreation.addAttribute("flagName", "updateAttitudesMessageCreation");
		updateAttitudesMessageCreation.addAttribute("value", "false");
		Element onDecisionRequest = activeContext.addElement("randomFlag");
		onDecisionRequest.addAttribute("flagName", "onDecisionRequest");
		onDecisionRequest.addAttribute("value", "false");
		Element updateBeliefs = activeContext.addElement("randomFlag");
		updateBeliefs.addAttribute("flagName", "updateBeliefs");
		updateBeliefs.addAttribute("value", "false");

		Element trace = randomFlags.addElement("Trace");
		Element computeStakeholders = trace.addElement("randomFlag");
		computeStakeholders.addAttribute("flagName", "computeStakeholders");
		computeStakeholders.addAttribute("value", "false");

		//Flags
		Element flags = root.addElement("flags");
		Element isSendingActiveNode = flags.addElement("flag");
		isSendingActiveNode.addAttribute("flagName", "isSendingActive");
		isSendingActiveNode.addAttribute("value", "true");
		Element isExperientialLearning = flags.addElement("flag");
		isExperientialLearning.addAttribute("flagName", "isExperientialLearning");
		isExperientialLearning.addAttribute("value", "false");


		try {
			cfWriter.write( doc );
			System.out.println("General is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}
	
	public static void buildCF() throws IOException{
		File outputFile = new File(outputFolder,outputFolder.getName()+"Cf.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("cfParameters");

		Element cf = root.addElement("cognitiveFramework");
		cf.addAttribute("isAttitudeRestricted", "true");
		cf.addAttribute("isBeliefRestricted", "true");
		cf.addAttribute("nt0", "0.7");
		cf.addAttribute("isNtRestricted", "false");
		cf.addAttribute("ntMin", "0.7");
		cf.addAttribute("delta", "0.6");//formely 0.33
		cf.addAttribute("epsilon", "0.0");
		cf.addAttribute("gamma", "0.8");
		cf.addAttribute("lambda", "2.0");
		cf.addAttribute("omega", "0.2");
		cf.addAttribute("phi", "0.7");//formely 0.8
		cf.addAttribute("rho", "0.9");//formely 0.8
		cf.addAttribute("sigma", "7");
		cf.addAttribute("tau", "0.1");
		cf.addAttribute("k", "2");
		cf.addAttribute("defaultAttitude", "0.2");
		cf.addAttribute("isRandomAttitude", "true");
		cf.addAttribute("randomAttitudeAllowance", "0.1");
		cf.addAttribute("defaultBelief", "0.2");
		cf.addAttribute("defaultSalience", "0.2");
		cf.addAttribute("defaultCapability", "0.2");
		cf.addAttribute("woundedActorPayoff", "-0.5");
		cf.addAttribute("killedActorPayoff", "-1");
		cf.addAttribute("t", "0.5");
		cf.addAttribute("u", "0.2");
		cf.addAttribute("s", "0.7");
		cf.addAttribute("mu", "0.2");
		cf.addAttribute("communicationReceptionUpdatePeriod", "3600.0");
		cf.addAttribute("communicationSendingUpdatePeriod", "3600.0");
		cf.addAttribute("experientialLearningUpdatePeriod", "3600.0");
		cf.addAttribute("communicationSimulation", "true");
		cf.addAttribute("stakeholderComputation", "true");
		cf.addAttribute("indicatorUpdatePeriod", "3600.0");
		cf.addAttribute("blueForceActionsFiltering", "true");
		cf.addAttribute("forceCompensationAcceptance", "true");
		cf.addAttribute("defaultTrust", "0.3");
		cf.addAttribute("defaultFrequency", "0.3");
		cf.addElement("groupTrust");

		try {
			//Write the document we've created
			cfWriter.write( doc );

			System.out.println("CF is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}
	
	public static void buildContext() throws IOException{
		File outputFile = new File(outputFolder,outputFolder.getName()+"Context.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("contextParameters");

		//Event Impact Levels
		Element eventImpactLevels = root.addElement("eventImpactLevels");
		Element eventImpactLevel = eventImpactLevels.addElement("eventImpactLevel");
		eventImpactLevel.addAttribute("name", "eventImpactLevelName");
		eventImpactLevel.addComment("to change");

		//Roles
		Element roles = root.addElement("roles");
		addRole(roles, "local", null);
		addRole(roles, "isaf", null);
		addRole(roles, "taliban", null);
		addRole(roles, "goira", null);
		addRole(roles, "media", null);

		//Action Models
		Element actionModels = root.addElement("actionModels");
		String[] roleNames = {"local", "isaf", "taliban", "goira"};
		addActionModel(actionModels, "accept", "accept", roleNames, null);
		addActionModel(actionModels, "reject", "reject", roleNames, null);
		addActionModel(actionModels, "ask isaf for protection", "", roleNames, null);
		addActionModel(actionModels, "talk to taliban", "", roleNames, null);
		addActionModel(actionModels, "ask goira for protection", "", roleNames, null);
		addActionModel(actionModels, "set-up an alp", "", roleNames, null);
		addActionModel(actionModels, "do nothing", "", roleNames, null);
		addActionModel(actionModels, "allow taliban", "", roleNames, null);
		addActionModel(actionModels, "join taliban", "", roleNames, null);
		addActionModel(actionModels, "do nothing in recruitment", "", roleNames, null);


		//Context Models
		Element contexts = root.addElement("contexts");
		
		//ContextModel 1 : property
		Element propertyContextModelNode =addContext(contexts, "property", "eventImpactLevelName", null);
		String[] actionsOfPropertyContextModel = {"accept", "reject"};
		addContextModelRoleSalience(propertyContextModelNode, "local", "0.25", actionsOfPropertyContextModel, null);
		addContextModelRoleSalience(propertyContextModelNode, "isaf", "0.25", null, null);
		addContextModelRoleSalience(propertyContextModelNode, "taliban", "0.25", null, null);
		addContextModelRoleSalience(propertyContextModelNode, "goira", "0.25", null, null);
		addContextModelRoleSalience(propertyContextModelNode, "media", "0.25", null, null);
		//Action Model
		Element contextActionModelsOfPropertyContextModel = propertyContextModelNode.addElement("actionModels");
		addContextModelActionModel(contextActionModelsOfPropertyContextModel, "accept");
		addContextModelActionModel(contextActionModelsOfPropertyContextModel, "reject");
		//Payoffs and actions
		Element payoffs1 = propertyContextModelNode.addElement("payoffs");
		addPayoff(payoffs1, "local", "0.33", "", "accept", null);
		addPayoff(payoffs1, "isaf", "0.25", "", "accept", null);
		addPayoff(payoffs1, "taliban", "-0.25", "", "accept", null);
		addPayoff(payoffs1, "goira", "0.25", "", "accept", null);
		
		addPayoff(payoffs1, "local", "-0.25", "", "reject", null);
		addPayoff(payoffs1, "isaf", "-0.25", "", "reject", null);
		addPayoff(payoffs1, "taliban", "0.25", "", "reject", null);
		addPayoff(payoffs1, "goira", "-0.25", "", "reject", null);
		
		
		//ContextModel 2 : security
		Element securityContextModelNode =addContext(contexts, "security", "eventImpactLevelName", null);
		String[] actions2 = {"ask isaf for protection", "talk to taliban", "ask goira for protection", "set-up an alp", "do nothing"};
		addContextModelRoleSalience(securityContextModelNode, "local", "0.25",actions2, null);
		addContextModelRoleSalience(securityContextModelNode, "isaf", "0.25",null, null);
		addContextModelRoleSalience(securityContextModelNode, "taliban", "0.25",null, null);
		addContextModelRoleSalience(securityContextModelNode, "goira", "0.25",null, null);	
		addContextModelRoleSalience(securityContextModelNode, "media", "0.25",null, null);	
		//ActionModels
		Element actionModelOfSecurityContext = propertyContextModelNode.addElement("actionModels");
		addContextModelActionModel(actionModelOfSecurityContext, "ask isaf for protection");
		addContextModelActionModel(actionModelOfSecurityContext, "talk to taliban");
		addContextModelActionModel(actionModelOfSecurityContext, "ask goira for protection");
		addContextModelActionModel(actionModelOfSecurityContext, "set-up an alp");
		addContextModelActionModel(actionModelOfSecurityContext, "do nothing");
		Element payoffs2 = securityContextModelNode.addElement("payoffs");
		addPayoff(payoffs2, "local", "0.10", "", "ask isaf for protection", null);
		addPayoff(payoffs2, "isaf", "0.25", "", "ask isaf for protection", null);
		addPayoff(payoffs2, "taliban", "-0.25", "", "ask isaf for protection", null);
		addPayoff(payoffs2, "goira", "0.25", "", "ask isaf for protection", null);
		
		addPayoff(payoffs2, "local", "0.25", "", "talk to taliban", null);
		addPayoff(payoffs2, "isaf", "-0.25", "", "talk to taliban", null);
		addPayoff(payoffs2, "taliban", "0.25", "", "talk to taliban", null);
		addPayoff(payoffs2, "goira", "-0.25", "", "talk to taliban", null);
		
		addPayoff(payoffs2, "local", "0.10", "", "ask goira for protection", null);
		addPayoff(payoffs2, "isaf", "-0.5", "", "ask goira for protection", null);
		addPayoff(payoffs2, "taliban", "-0.5", "", "ask goira for protection", null);
		addPayoff(payoffs2, "goira", "0.5", "", "ask goira for protection", null);
		
		addPayoff(payoffs2, "local", "0.5", "", "set-up an alp", null);
		addPayoff(payoffs2, "isaf", "-0.1", "", "set-up an alp", null);
		addPayoff(payoffs2, "taliban", "0.0", "", "set-up an alp", null);
		addPayoff(payoffs2, "goira", "-0.1", "", "set-up an alp", null);
		
		addPayoff(payoffs2, "local", "-0.33", "", "do nothing", null);
		addPayoff(payoffs2, "isaf", "0.0", "", "do nothing", null);
		addPayoff(payoffs2, "taliban", "0.1", "", "do nothing", null);
		addPayoff(payoffs2, "goira", "0.0", "", "do nothing", null);
		
		//ContextModel 3 : recruitment
		Element contextModel3 =addContext(contexts, "recruitment", "eventImpactLevelName", null);
		String[] actions3 = {"allow taliban", "join taliban", "do nothing in recruitment"};
		addContextModelRoleSalience(contextModel3, "local", "0.25",actions3, null);
		addContextModelRoleSalience(contextModel3, "isaf", "0.25",null, null);
		addContextModelRoleSalience(contextModel3, "taliban", "0.25",null, null);
		addContextModelRoleSalience(contextModel3, "goira", "0.25",null, null);
		//ActionModels
		Element actionModelOfRecruitmentContext = propertyContextModelNode.addElement("actionModels");
		addContextModelActionModel(actionModelOfRecruitmentContext, "allow taliban");
		addContextModelActionModel(actionModelOfRecruitmentContext, "join taliban");
		addContextModelActionModel(actionModelOfRecruitmentContext, "do nothing in recruitment");
		Element payoffs3 = contextModel3.addElement("payoffs");
		addPayoff(payoffs3, "local", "0.0", "", "allow taliban", null);
		addPayoff(payoffs3, "isaf", "-0.75", "", "allow taliban", null);
		addPayoff(payoffs3, "taliban", "0.75", "", "allow taliban", null);
		addPayoff(payoffs3, "goira", "-0.75", "", "allow taliban", null);
		
		addPayoff(payoffs3, "local", "0.0", "", "join taliban", null);
		addPayoff(payoffs3, "isaf", "-0.5", "", "join taliban", null);
		addPayoff(payoffs3, "taliban", "0.5", "", "join taliban", null);
		addPayoff(payoffs3, "goira", "-0.5", "", "join taliban", null);
		
		addPayoff(payoffs3, "local", "0.0", "", "do nothing in recruitment", null);
		addPayoff(payoffs3, "isaf", "-0.0", "", "do nothing in recruitment", null);
		addPayoff(payoffs3, "taliban", "0.0", "", "do nothing in recruitment", null);
		addPayoff(payoffs3, "goira", "-0.0", "", "do nothing in recruitment", null);
		
		//ContextModel 4 : demographics
		Element contextModel4 =addContext(contexts, "demographics", "eventImpactLevelName", null);
		addContextModelRoleSalience(contextModel4, "local", "0.25",null, null);
		addContextModelRoleSalience(contextModel4, "isaf", "0.25",null, null);
		addContextModelRoleSalience(contextModel4, "taliban", "0.25",null, null);
		addContextModelRoleSalience(contextModel4, "goira", "0.25",null, null);		
		addContextModelRoleSalience(contextModel4, "media", "0.25",null, null);		
		addEmptyPayoffToContextModel(contextModel4);

		try {
			//Write the document we've created
			cfWriter.write( doc );

			System.out.println("Context is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}


	public static void buildActors() throws IOException{
		File outputFile = new File(outputFolder,outputFolder.getName()+"Actor.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("actorParameters");

		HashSet<String> register = new HashSet<String>();
		BufferedReader br = null;
		//Force Actors
		Element forceActors = root.addElement("forceActors");
		addForceActor(forceActors, "isaf", "isaf");
		addForceActor(forceActors, "taliban", "taliban");
		addForceActor(forceActors, "goira", "goira");
		

		//Actors
		Element actors = root.addElement("actors");

		br = new BufferedReader(new FileReader(new File(dataInput)));
		String line;
		String[] split;
		br.readLine();
		while((line = br.readLine()) != null) {
			split = line.split(";");

			//Create the first actor of the couple
			if(!register.contains(split[0])){
				Element actor = actors.addElement("actor");
				actor.addAttribute("name", split[0]);
				actor.addAttribute("role", "local");
				actor.addElement("factList");
				Element ltm = actor.addElement("memory");
				Element contexts = ltm.addElement("contexts");
//				ltm.addElement("saliences");
//				ltm.addElement("attitudes");
//				ltm.addElement("beliefs");
//				ltm.addElement("decisionCounters");
//				ltm.addElement("decisionCounts");
				addActorContext(contexts, "security");
				addActorContext(contexts, "property");
				addActorContext(contexts, "recruitment");
				addActorContext(contexts, "demographics");
				
				register.add(split[0]);
			}

			//Create the second actor of the couple
			if(!register.contains(split[1])){
				Element actor = actors.addElement("actor");
				actor.addAttribute("name", split[1]);
				actor.addAttribute("role", "local");
				actor.addElement("factList");
				Element ltm = actor.addElement("memory");
				Element contexts = ltm.addElement("contexts");
//				ltm.addElement("saliences");
//				ltm.addElement("attitudes");
//				ltm.addElement("beliefs");
//				ltm.addElement("decisionCounters");
//				ltm.addElement("decisionCounts");
				addActorContext(contexts, "security");
				addActorContext(contexts, "property");
				addActorContext(contexts, "recruitment");
				addActorContext(contexts, "demographics");
				register.add(split[1]);
			}
		}
		br.close();

		try {
			//Write the document we've created
			cfWriter.write( doc );

			System.out.println("Actor is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}
	
	public static void buildContact() throws IOException{
		File outputFile = new File(outputFolder,outputFolder.getName()+"Contact.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("contactParameters");

		//Contacts
		BufferedReader br = null;
		br = new BufferedReader(new FileReader(new File(dataInput)));
		String line;
		String[] split;
		br.readLine();
		Element contacts = root.addElement("contacts");
		while((line = br.readLine()) != null) {
			split = line.split(";");
			//Create the contact
			Element contact = contacts.addElement("contact");
			contact.addAttribute("owner", split[0]);
			contact.addAttribute("target", split[1]);
			contact.addAttribute("mu", split[3].replace(",", "."));
			contact.addAttribute("trust", split[4].replace(",", "."));
			contact.addAttribute("frequency", split[2].replace(",", "."));
		}
		br.close();

		try {
			//Write the document we've created
			cfWriter.write( doc );

			System.out.println("Contact is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}
	
	public static void buildEvent() throws IOException{
		File outputFile = new File(outputFolder,outputFolder.getName()+"Event.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("eventParameters");

		//Events
		Element events = root.addElement("events");
		
		Element event1 = addEvent(events, "accepted in property event", "", "property", "1", "true", "1231", "", "1230", null);
		addactorActionRolePayoffToEvent(event1, "1230", "accept", "local", "0.25");
		addactorActionRolePayoffToEvent(event1, "1260", "", "local", "0.0");
		addactorActionRolePayoffToEvent(event1, "isaf", "", "isaf", "0.0");
		addactorActionRolePayoffToEvent(event1, "taliban", "", "taliban","0.0");
		addactorActionRolePayoffToEvent(event1, "goira", "", "goira", "0.0");
		
		Element event2 = addEvent(events, "passive event in property", "", "property", "2", "false", "1231", "", "", null);
		addactorActionRolePayoffToEvent(event2, "1230", "", "local", "-0.25");
		addactorActionRolePayoffToEvent(event2, "1260", "", "local", "-0.25");
		addactorActionRolePayoffToEvent(event2, "isaf", "", "isaf", "0.0");
		addactorActionRolePayoffToEvent(event2, "taliban", "", "taliban","0.0");
		addactorActionRolePayoffToEvent(event2, "goira", "", "goira", "0.0");
		
		Element event3 = addEvent(events, "Set-up an ALP event in security", "", "security", "3", "true", "1231", "", "1230", null);
		addactorActionRolePayoffToEvent(event3, "1230", "set-up an alp", "local", "-0.1");
		addactorActionRolePayoffToEvent(event3, "1260", "", "local", "-0.1");
		addactorActionRolePayoffToEvent(event3, "isaf", "", "isaf", "0.0");
		addactorActionRolePayoffToEvent(event3, "taliban", "", "taliban","0.0");
		addactorActionRolePayoffToEvent(event3, "goira", "", "goira", "0.0");
		
		Element event4 = addEvent(events, "passive event in demographics", "", "demographics", "4", "false", "1231", "", "", null);
		addactorActionRolePayoffToEvent(event4, "1230", "", "local", "-1");
		addactorActionRolePayoffToEvent(event4, "1260", "", "local", "-0.5");
		addactorActionRolePayoffToEvent(event4, "isaf", "", "isaf", "0.0");
		addactorActionRolePayoffToEvent(event4, "taliban", "", "taliban","0.0");
		addactorActionRolePayoffToEvent(event4, "goira", "", "goira", "0.0");
		
		Element event5 = addEvent(events, "allowing taliban", "", "recruitment", "5", "true", "1231", "", "1230", null);
		addactorActionRolePayoffToEvent(event5, "1230", "allow taliban", "local", "0.0");
		addactorActionRolePayoffToEvent(event5, "1260", "", "local", "0.0");
		addactorActionRolePayoffToEvent(event5, "isaf", "", "isaf", "-1.0");
		addactorActionRolePayoffToEvent(event5, "taliban", "", "taliban","1.0");
		addactorActionRolePayoffToEvent(event5, "goira", "", "goira", "-1.0");
		
		Element event6 = addEvent(events, "rejecting in property event in property", "", "property", "6", "true", "1231", "", "1230", null);
		addactorActionRolePayoffToEvent(event6, "1230", "reject", "local", "0.0");
		addactorActionRolePayoffToEvent(event6, "1260", "", "local", "0.0");
		addactorActionRolePayoffToEvent(event6, "isaf", "", "isaf", "-0.0");
		addactorActionRolePayoffToEvent(event6, "taliban", "", "taliban","0.0");
		addactorActionRolePayoffToEvent(event6, "goira", "", "goira", "-0.0");

		try {
			//Write the document we've created
			cfWriter.write( doc );

			System.out.println("Event is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}
	
	public static void buildMessage() throws IOException{
		File outputFile = new File(outputFolder,outputFolder.getName()+"Message.xml");
		outputFile.getParentFile().mkdirs();
		outputFile.createNewFile();
		XMLWriter cfWriter = new XMLWriter(new FileWriter(outputFile), OutputFormat.createPrettyPrint());
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("messageParameters");

		//Messages
		Element messages = root.addElement("messages");
		
		//Statement Messages
		Element statementMessages = messages.addElement("statementMessages");
		addAttitudeMessage(statementMessages, "an attitude msg", "1230", "100", "security", "1.0", "isaf");
		
		//Trace Messages
		Element traceMessages = messages.addElement("traceMessages");
		String[] eventNameTab = {"accepted in property event"/*"accepted", "passive event", "doing nothing in security", "passive event in demographics",
				"allowing taliban", "rejecting in property"*/};
		
		addTraceMessage(traceMessages, "a trace msg", "media", "100", "a trace", "false", "", eventNameTab);

		try {
			//Write the document we've created
			cfWriter.write( doc );

			System.out.println("Message is Done.");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(cfWriter != null)
				cfWriter.close();
		}
	}
	
	public static Element addTraceMessage(Element traceMessages, String msgName, String sender, String emitNumber,
			String traceName, String isPromise, String description, String[] eventNameTab){
		Element traceMessage = traceMessages.addElement("traceMessage");
		traceMessage.addAttribute("name", msgName);
		traceMessage.addAttribute("sender", sender);
		traceMessage.addAttribute("emitNumber", emitNumber);		
		addTrace(traceMessage, traceName, isPromise, description, eventNameTab);
		return traceMessage;		
	}
	
	public static Element addAttitudeMessage(Element statementMessages, String msgName, String sender, String emitNumber,
			String contextModel, String value, String target){
		Element msg = statementMessages.addElement("attitudeMessage");
		msg.addAttribute("name", msgName);
		msg.addAttribute("sender", sender);
		msg.addAttribute("emitNumber", emitNumber);		
		msg.addAttribute("contextModel", contextModel);
		msg.addAttribute("value", value);
		msg.addAttribute("target", target);		
		return msg;
	}
	
	public static Element addTrace(Element elt, String name, String isPromise, String description, String[] eventNameTab){
		Element trace = elt.addElement("trace");
		trace.addAttribute("isPromise", isPromise);
		trace.addAttribute("description", description);
		trace.addAttribute("name", name);
		for(int i=0; i < eventNameTab.length; i++) {
			Element event = trace.addElement("event");
			event.addAttribute("name", eventNameTab[i]);
		}
		return trace;
	}
	
	
	private static void addContextModelActionModel(Element actionModels,
			String actionModelName) {
		Element actionModel = actionModels.addElement("actionModel");
		actionModel.addAttribute("name", actionModelName);
	}
	
	
	public static void retriveArguments(String[] args){
		//Read arguments
		if(args.length>2){
			System.out.println("Too much arguments : expected [1]dataInput file path and [2]output file path");
			System.exit(1);
		} else if(args.length<2 && args.length>0){
			System.out.println("Too few arguments : expected [1]dataInput file path and [2]output file path");
			System.exit(1);
		}
		else if(args.length == 2){
			dataInput = args[0];
			
		}
	}
	
}
